package com.example.zwx.myapplication;

import android.content.Context;
import android.content.pm.PackageManager;
import android.util.Log;

/**
 * Created by zwx on 16-1-21.
 */
public class BleUtil {
    public static final String TAG = "BleUtil";
    public static final int FREQ = 37840;


    public static boolean isSupportBle(Context context) {
        return context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE);
    }

    public static byte[][] process(int[] irData, int freq) {

        if (freq == 0) {
            freq = FREQ;
        }
        int pulseFactor = 1000000 / freq;
        int[] result = new int[irData.length * 2];
        for (int i = 0; i < irData.length; i++) {

            if (i % 2 == 0) {
                result[2 * i] = ((irData[i] / pulseFactor) >> 8) + 0x80;
                result[2 * i + 1] = ((irData[i] / pulseFactor)) & 0xFF;
            } else {
                result[2 * i] = ((irData[i] / pulseFactor)) >> 8;
                result[2 * i + 1] = ((irData[i] / pulseFactor)) & 0xFF;
            }

        }

        int total = result.length + 8;
        int lineCount = total / 16;
        if (total % 16 > 0) {
            lineCount += 1;
        }
        Log.e(TAG, lineCount + ":  lineCount");

        int[] irdata = new int[lineCount * 16];
        //head
        irdata[0] = 0x78;
        irdata[1] = 0x56;
        irdata[2] = 0x34;
        irdata[3] = 0x12;
        //frequency
        irdata[4] = 0x26;
        irdata[5] = 0x00;

        System.arraycopy(result, 0, irdata, 6, result.length);
        //tail
        for (int i = (result.length + 6) - 1; i < irdata.length; i++) {
            irdata[i] = 0x00;
        }

        //        for (int i = 0; i < irdata.length; i++) {
        //            Log.e("irdata: " + irdata[i] + "");
        //        }

        Log.e(TAG, "irdata : length" + irdata.length);

        int[][] pkg = new int[lineCount][20];
        for (int i = 0; i < pkg.length; i++) {
            pkg[i][0] = 0x71;
            if (i == 0) {
                pkg[i][1] = 0x41;

            } else if (i > 0 && i < lineCount - 1) {
                pkg[i][1] = 0x51;
            } else {
                pkg[i][1] = 0x61;
            }

            if (i == 0) {
                pkg[i][2] = lineCount;
            } else if (i > 0 && i < lineCount) {
                pkg[i][2] = i + 1;
            }

            System.arraycopy(irdata, i * 16, pkg[i], 3, 16);
            int add = 0;
            for (int j = 0; j < pkg[i].length - 1; j++) {
                add += pkg[i][j];
            }
            pkg[i][19] = add & 0xFF;
        }
        byte[][] pkgBytes = new byte[pkg.length][pkg[0].length];//pkg.length = 9 pkg[0].length is the length of the line
        for (int i = 0; i < pkgBytes.length; i++) {
            for (int j = 0; j < pkgBytes[i].length; j++) {
                pkgBytes[i][j] = (byte) pkg[i][j];
            }
        }

        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < pkg.length; i++) {
            for (int j = 0; j < pkg[i].length; j++) {
                stringBuilder.append(String.format("0x%02x", pkg[i][j])).append(",");
            }
            stringBuilder.append("#");
        }
        Log.e(TAG, "pkgs :  " + stringBuilder.toString());
        return pkgBytes;
    }


}
